package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(path ="/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping("/list")
    public Result query(){
        List<Role> roleList =roleService.query("list");
        return Result.success(roleList);
    }
    @PutMapping
    public Result update(@RequestBody Role role) {
        log.info("修改角色:{}", role);
        roleService.update(role);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable ("id") Integer id){
        log.info("查询角色id:{}", id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除角色id:{}", id);
        roleService.deleteById(id);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Role role){
        log.info("新增角色:{}", role);
        roleService.add(role);
        return Result.success();
    }
    @GetMapping
    public Result page(
        @RequestParam(name ="name", required = false)String name,
        @RequestParam(name = "label", required = false)Integer label,
        @RequestParam(name = "page", defaultValue = "1")Integer page,
        @RequestParam(name = "pageSize", defaultValue = "10")Integer pageSize){
        PageResult<Role> pageResult = roleService.list(name, label, page, pageSize);
        log.info("分页查询部门,参数:角色名称={},角色标识={},当前页码={},每页大小={}", name, label, page, pageSize);
        return Result.success(pageResult);
    }
}
